package com.pomelo.tiku.activities

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.pomelo.tiku.R
import com.pomelo.tiku.adapters.BaseAdapter
import com.pomelo.tiku.adapters.FuncationAdapter
import kotlinx.android.synthetic.main.activity_vacation_select.*
import kotlinx.android.synthetic.main.layout_item_vacation_view.view.*
import android.content.Intent
import com.blankj.utilcode.util.LogUtils


class VacationSelectActivity : BaseActivity() {
    private val RESULT_CODE = 100
    var listData: ArrayList<String>? = null
    var listView: ArrayList<View>? = null
    var hashMap: HashMap<String,List<String>>? = null
    var listFunction: ArrayList<String> ? = null
    private var mAdapter: FuncationAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vacation_select)
        initView()
        if(listView != null && listView!!.size > 0) {
            listView!![0].tv_name.setTextColor(resources.getColor(R.color.home_tab_txt_blue_color))
        }
    }

    private fun initView() {
        hashMap = HashMap()
        listData = ArrayList()
        listView = ArrayList()
        listFunction = ArrayList()

        listData!!.add("技术类")
        listData!!.add("设计类")
        listData!!.add("行政类")
        listData!!.add("财务类")
        listData!!.add("销售类")
        listData!!.add("管理类")

        for(index in 0 until listData!!.size) {
            val value = ArrayList<String>()
            val key = listData!![index]
            value.add("aaaaa$index" )
            value.add("bbbbb$index" )
            value.add("ccccc$index" )
            value.add("ddddd$index" )
            value.add("eeeee$index" )
            hashMap!![key] = value
        }

        for(index in 0 until listData!!.size) {
            addView(item_vacation_container, listData!![index])
        }
        hashMap!![listData!![0]]?.let { listFunction!!.addAll(it) }
        listFunction?.let { initRecyclerView(it) }
    }

    private fun initRecyclerView(list: List<String>) {
        mAdapter = FuncationAdapter(list)
        val manager = GridLayoutManager(this,2)
        rv.layoutManager = manager
        rv.adapter = mAdapter
        mAdapter!!.setOnItemClickListener(object: BaseAdapter.OnItemClick<String>{
            override fun OnItemClick(pos: Int, data: String?) {
                if (data != null) {
                    finishWithResult(data)
                }
            }
        })
    }

    private fun addView(viewGroup: ViewGroup,text: String) {
        val view = LayoutInflater.from(this).inflate(R.layout.layout_item_vacation_view,null)
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,120)
        params.gravity = Gravity.CENTER
        view.layoutParams = params
        view.tv_name.text = text
        listView!!.add(view)
        viewGroup.addView(view)
        view.setOnClickListener {
            if(listView != null && listView!!.size > 0) {
                for(index in 0 until listView!!.size) {
                    listView!![index].tv_name.setTextColor(resources.getColor(R.color.black))
                }
            }
            view.tv_name.setTextColor(resources.getColor(R.color.home_tab_txt_blue_color))
            listFunction = hashMap!![view.tv_name.text.toString()] as ArrayList<String>?
            mAdapter!!.refreshAdapter(listFunction!!)
        }
    }
    private fun finishWithResult(value: String) {
        val intent = Intent()
        intent.putExtra("data_function", value)
        setResult(RESULT_CODE, intent)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        listView = null
        listData = null
    }
}
